
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace eCom.Gui
{

	public class CreateNewCommunityCompanyWizardBizProc : BaseBusinessProcess
	{
		public CreateNewCommunityCompanyWizardBizProc(ActiveBizProc procState)
			: base(procState)
		{
			if (State.InProgressAddBizObj == null)
				State.InProgressAddBizObj = App.bizObjMgr.create("SignUpInfo");
		}
		
		protected override void setupRuleNames()
		{
			base.m_updateBizObjRule = "processNewChildCompanySignUp";
		}

		public override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Edit; }
		}
		
		public override IResults updateBizObj(IBizObj obj)
		{
			IResults rc = null;
			IBizObj wizardInfo = m_procState.get("info", false) as IBizObj;
			Debug.Assert(wizardInfo != null);

			if (requiredFieldMissing(new string[] { "name", "shortName"}, obj.get("companyInfo") as IBizObj))
			{
				if (wizardInfo != null)
					wizardInfo.put("stepCompleted", false);
				return rc;
			}

			if (!fieldValueIsMatch(obj.get("rootUserInfo.password"),obj.get("rootUserInfo.password2")))
			{
				if (wizardInfo != null)
					wizardInfo.put("stepCompleted", false);
				return rc;
			}

			if (!(bool)wizardInfo.get("stepCompleted") &&
				!Cmn.IsEmpty(obj.get("companyInfo.name")) && !Cmn.IsEmpty(obj.get("companyInfo.shortName")) &&
				!Cmn.IsEmpty(obj.get("rootUserInfo.userName")) && !Cmn.IsEmpty(obj.get("rootUserInfo.password")))
			{
				//When the community is built from a combined set of products (like membership type and CCM type)
				//Then the orderItemCombinedPartId field will be set, otherwise use the standard product.partId.
				string partId = (string)wizardInfo.get("orderItemCombinedPartId");
				if(Cmn.IsEmpty(partId))
					partId = (string)wizardInfo.get("product.partId");

				obj.put("productKey", partId);
				obj.put("salesOrderItemId", wizardInfo.get("salesOrderItemId"));
				rc = base.updateBizObj(obj);
				eCom.SignUpResult results = rc.CallResults as eCom.SignUpResult;
				if (rc != null && results != null)
				{
					if (results.EComSignUpResultInfo == eComSignUpResult.Success)
						wizardInfo.put("stepCompleted", true);
					else if (results.EComSignUpResultInfo == eComSignUpResult.InvalidShortName)
						displayMessage("Error-CompanyShortNameAlreadyTaken");
					else
						displayMessage("Error-CommunityCompanyCreationGeneralFailure");
				}
			}
			return rc;

		}
		protected bool requiredFieldMissing(string[] fields, IBizObj obj)
		{
			for (int i = 0; i < fields.Length; i++)
				if (Cmn.IsEmpty(obj.get(fields[i])))
				{
					m_procState.BPC.Messenger.add("Error-RequiredFieldMissing", null);
					return true;
				}
			return false;
		}
		protected bool fieldValueIsMatch(object value1, object value2)
		{
			if (string.Compare(value1.ToString(), value2.ToString()) != 0)
			{
				m_procState.BPC.Messenger.add("Error-RequiredFieldMissing", null);
				return false;
			}
			return true;
		}

		
	}
}
